<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>课程管理</title>
    <!--vue-->
    <script src="/js/vue.js"></script>
    <!-- 引入样式 -->
    <link rel="stylesheet" href="/css/index.css">
    <!-- 引入组件库 -->
    <script src="/js/index.js"></script>
    <!--axios-->
    <script src="/js/axios.min.js"></script>
</head>
<body>
<div id="app">
    <el-row>
        <el-col :span="4"><el-input v-model="coursename"   size="mini" placeholder="课程名称"></el-input></el-col>
        <el-col :span="4"><el-button type="primary" size="mini" @click="search()" icon="el-icon-search">搜索</el-button></el-col>
        <el-col :span="12">&nbsp;</el-col>
        <el-col :span="2"><el-button type="primary" size="small" round @click="toEdit()" icon="el-icon-circle-plus">添加</el-button></el-col>
        <el-col :span="2"><el-button type="primary" size="mini" @click="flush()" icon="el-icon-search">刷新</el-button></el-col>

    </el-row>
    <el-row>
        <el-table
                :data="tableData"
                style="width: 100%">
            <el-table-column
                    prop="courseid"
                    label="课程ID"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="coursename"
                    label="课程名称"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="coursetype"
                    label="课程类型">
            </el-table-column>
            <el-table-column
                    prop="coursedir"
                    label="课程方向">
            </el-table-column>
            <el-table-column
                    prop="positionid"
                    label="职位ID">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button
                            type="primary" size="small" round
                            @click="toEdit(scope.row.courseid)"class="el-icon-edit">编辑</el-button>
                    <el-button
                            size="small" round
                            type="danger"
                            @click="handleDelete(scope.$index, scope.row)"class="el-icon-delete"> 删除</el-button>
                </template>
            </el-table-column>
        </el-table>
    </el-row>
    <el-row>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[2,3,5,10,100, 200, 300, 400]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>
    </el-row>
    <el-row>
        <!--更新弹出框-->
        <el-dialog title="编辑" :visible.sync="editFormVisible">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item v-if="show" label="课程ID" :label-width="formLabelWidth">
                    <el-input v-model="form.courseid" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程名称" :label-width="formLabelWidth" prop="coursename">
                    <el-input v-model="form.coursename" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程类型" :label-width="formLabelWidth" prop="coursetype">
                    <el-input v-model="form.coursetype" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="课程方向" :label-width="formLabelWidth" prop="coursedir">
                    <el-input v-model="form.coursedir" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item label="职位ID" :label-width="formLabelWidth" prop="positionid">
                    <el-input v-model="form.positionid" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="editFormVisible = false">取 消</el-button>
                <el-button v-if="form.courseid==null" type="primary" @click="add();">添 加</el-button>
                <el-button v-else type="primary" @click="update()">修 改</el-button>
            </div>
        </el-dialog>
    </el-row>
</div>
<script>
    var vue = new Vue({
        el:"#app",
        data:{//数据
            tableData:[],
            currentPage:1,
            pageSize:5,
            total:20,
            trainerId:null,
            coursename:null,//搜索框
            editFormVisible:false,//编辑对话框是否显示
            form:{
                coursename:'',
                coursetype:'',
                coursedir: '',
                positionid:''
            },//添加或者更新form
            formLabelWidth:"120px",
            imageUrl: '',
            rules: {
                coursename: [
                    {required: true, message: '请输入课程名称', trigger: 'blur'},
                    {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
                ],
                coursetype: [
                    {required: true, message: '请输入课程类型', trigger: 'blur'},
                    {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
                ],
                coursedir: [
                    {required: true, message: '请输入课程方向', trigger: 'blur'},
                    {min: 2, max: 6, message: '长度在 2 到 6 个字符', trigger: 'blur'}
                ],
                positionid: [
                    {required: true, message: '请输入职位ID', trigger: 'blur'},
                    {min: 1, max: 4, message: '长度在 1 到 4 个字符', trigger: 'blur'}
                ],

            },
        },
        created:function () {
            this.loadData();
        },
        methods:{//方法
            //加载数据的方法
            loadData:function () {
                _this =this;
                //初始化数据
                axios.get('/course/page', {
                    params: {
                        pageNo:this.currentPage,
                        pageSize:this.pageSize,
                        coursename:this.coursename
                    }
                })
                    .then(function (response) {
                        //赋值分页列表
                        _this.tableData = response.data.page;
                        //删除最后一页为空时，调到上一页
                        if(_this.tableData.length==0&&_this.currentPage!=1){
                            _this.currentPage=_this.currentPage-1;
                            _this.loadData();
                        }
                        //赋值总条数
                        _this.total = response.data.total;
                    })
                    .catch(function (error) {
                        //console.log(error);
                    })
                    .then(function () {
                        // always executed  总是执行  相当于finally
                    });
            },
            flush(){
                //刷新当前页
                location.reload();
            },
            //每页数量发生变化时
            handleSizeChange:function (v) {
                this.pageSize=v;
                this.loadData();
            },
            //当前页数放生变化是
            handleCurrentChange:function (v) {
                this.currentPage=v;
                this.loadData();
            },
            // 搜索方法
            search:function () {
                this.currentPage=1;
                this.loadData();
            },
            //跳转编辑
            toEdit:function (courseid) {
                console.log("我进来了");
                if(courseid > 0){
                    this.form={};
                    //根据id请求
                    axios.post('/course/onelist?courseid='+courseid).then(function (response) {
                        _this.form = response.data.list;
                    })
                }else {
                    this.form={
                        coursename:'',
                        coursetype:'',
                        coursedir: '',
                        positionid:''
                    };
                }
                this.editFormVisible=true;
            },
            //删除
            handleDelete:function (index,row) {
                this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.post('/course/delete?courseId='+row.courseid).then(function (response) {
                        console.log(response);
                        if(response.status == 200){
                            if(response.data>0) {
                                //提示成功
                                _this.$message({
                                    type: 'success',
                                    message: '删除成功!'
                                });
                                //重新加载数据
                                _this.loadData();
                            }
                        }
                    }).catch(function (error) {
                        console.log(error);
                        _this.$message.error("你错了。。。");
                    });

                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            //添加
            add:function(){
                _this = this;
                if(vue.form.coursename==''||_this.form.coursetype==''||_this.form.coursedir==''||_this.form.positionid==''){
                    _this.$message.error("信息未填充完整！");
                    return false;
                }
                axios.post('/course/add',this.form).then(function (response) {
                    console.log(response);
                    if(response.status == 200){
                        //提示成功
                        _this.$message({
                            message: '恭喜你，添加成功',
                            type: 'success'
                        });
                        //关闭弹出框
                        _this.editFormVisible=false;
                        //重新加载数据
                        _this.loadData();
                        //清空form
                        _this.form={};
                    }
                }).catch(function (error) {
                    console.log(error);
                    _this.$message.error("你错了。。。");
                });
            },
            //更新
            update:function(){
                _this = this;
                axios.post('/course/update',this.form).then(function (response) {
                    console.log(response);
                    if(response.status == 200){
                        //提示成功
                        _this.$message({
                            message: '恭喜你，操作成功',
                            type: 'success'
                        });
                        //关闭弹出框
                        _this.editFormVisible=false;
                        //重新加载数据
                        _this.loadData();
                    }
                }).catch(function (error) {
                    console.log(error);
                    _this.$message.error("你错了。。。");
                });
            }
        }
    });
</script>
</body>
</html>